using System.Collections;
using System.Collections.Generic;
using TEngine;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace GameLogic
{
    public class OpeningDragItem : MonoBehaviour,IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        Image m_Image;
        // Start is called before the first frame update
        void Start()
        {
            m_Image = transform.FindChildComponent<Image>("Icon");
        }

        // Update is called once per frame
        void Update()
        {
        
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            m_Image.transform.SetParent(m_Image.canvas.transform);
            eventData.selectedObject = m_Image.gameObject;
        }

        public void OnDrag(PointerEventData eventData)
        {
            m_Image.rectTransform.localPosition = Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2);
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if(eventData.selectedObject)
            {
                m_Image.transform.SetParent(transform);
                m_Image.transform.localPosition = Vector3.zero;
            }
            else
            {
                m_Image.gameObject.SetActive(false);
            }
        }
    }
}
